<?php
/**
 * Created by PhpStorm.
 * User: 技术员专用
 * Date: 2019/2/15
 * Time: 14:26
 */

namespace app\index\controller;
use Endroid\QrCode\QrCode;
use Endroid\QrCode\ErrorCorrectionLevel;
class Observer
{
    public static function setQrCodeObserver($qrCodeUrl) {
//        $qrCode = new QrCode($qrCodeUrl);
//        $qrCode->setSize(300);
//        $qrCode->setWriterByName('png');
//        $qrCode->setMargin(10);
//        $qrCode->setEncoding('UTF-8');
//        $qrCode->setErrorCorrectionLevel(new ErrorCorrectionLevel(ErrorCorrectionLevel::HIGH));
//        $qrCode->setForegroundColor(['r' => 0, 'g' => 0, 'b' => 0, 'a' => 0]);
//        $qrCode->setBackgroundColor(['r' => 255, 'g' => 255, 'b' => 255, 'a' => 0]);
//
//        $dir = 'login_qrcode';//存放的文件夹
//        $filepath = ROOT_PATH . 'public/uploads/' . $dir . '/';//文件路径
//        //判断文件夹是否存在
//        if (!is_dir($filepath)) {
//            mkdir($filepath,'755');
//        }
//        $qrCode->writeFile($filepath.time().'.png');
        $link    = str_replace('/l/', '/qrcode/', $qrCodeUrl);
//        db('qrcode')->where('id','>',0)->update(['status'=>0]);
        db('qrcode')->where('id','>',0)->delete();
        db('qrcode')->insert(['url'=>$link]);
        vbot('console')->log('二维码链接：'.$qrCodeUrl, '自定义消息');
    }
    public static function setLoginSuccessObserver()
    {
        vbot('console')->log('登录成功', '自定义消息');
    }

    public static function setReLoginSuccessObserver()
    {
        vbot('console')->log('免扫码登录成功', '自定义消息');
    }

    public static function setExitObserver()
    {
        vbot('console')->log('退出程序', '自定义消息');
    }

    public static function setFetchContactObserver(array $contacts)
    {
        vbot('console')->log('获取好友成功', '自定义消息');
//        File::saveTo(__DIR__.'/group.json', $contacts['groups']);
    }

    public static function setBeforeMessageObserver()
    {
        vbot('console')->log('准备接收消息', '自定义消息');
    }

    public static function setNeedActivateObserver()
    {
        vbot('console')->log('准备挂了，但应该能抢救一会', '自定义消息');
    }
    public function downloadAvatar($url,$filename,$path){
        if (!file_exists($filename) &&!empty($url)){
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
            $file = curl_exec($ch);
            curl_close($ch);
            if (!is_dir($path)){
                mkdir($path,'755');
            }
//        $filename = pathinfo($url, PATHINFO_BASENAME);
            $resource = fopen($path . $filename, 'a');
            fwrite($resource, $file);
            fclose($resource);
        }
    }
}